<!--  Ofri Keidar 302933981 group 05 -->

<%@page import="view.WebCommons"%>
<%@page import="model.database.DBException"%>
<%@page import="controller.AdministratorController"%>
<%@page import="controller.CommonValues"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="employeeData" class="view.useBeans.EmployeeData"
	scope="page" />
<jsp:setProperty name="employeeData" property="*" />

<html>
<head>
<link rel="stylesheet" href="../../style/myTemplate/style.css"
	type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add new employee</title>
</head>
<body>

	<span class="customHeader2">Add a new employee</span>

	<div id="page">
		<div class="post">

			<div id="sidebar">
				<ul>
					<li>
						<h2>Please insert new employee's personal details</h2>
					</li>
				</ul>
			</div>
	
			<div id="content">
				<form METHOD=POST ACTION="AddEmployee.jsp">
					<table>
						<tr>
							<td><span class="customFormValue">Number</span></td>
							<td><INPUT TYPE=TEXT NAME=number
								SIZE=<%=WebCommons.NUMBER_SIZE%>></td>
						</tr>
						<tr>
							<td><span class="customFormValue">Name</span></td>
							<td><INPUT TYPE=TEXT NAME=name SIZE=<%=WebCommons.NAME_SIZE%>></td>
						</tr>
						<tr>
							<td><span class="customFormValue">Username</span></td>
							<td><INPUT TYPE=TEXT NAME=userName
								SIZE=<%=WebCommons.NAME_SIZE%>></td>
						</tr>
					</table>
					<p><span class="customFormValue">What would be the rank? <br>Employee <INPUT TYPE="radio"
						NAME=permission
						VALUE="<%=CommonValues.permissions.EMPLOYEE.toString()%>"
						CHECKED="checked" SIZE=20> Manager <INPUT TYPE="radio"
						NAME=permission
						VALUE="<%=CommonValues.permissions.MANAGER.toString()%>" SIZE=20></span></p>
	
					<br> <INPUT TYPE=SUBMIT class="mySubmitButton">
				</form>
			</div>
	
		</div>

	</div>
	
	<div id="footer">
		<span class="myLink"><A HREF="../../menus/AdminMenu.jsp">Back to Menu</A></span>
	</div>
	
	<%
		//check if already submitted details
		if (employeeData.isDataSet()) {

			// check if numeric value was given
			Integer employeeNumber = employeeData.getNumberValue();
			if (employeeNumber == null) {
				
				// notify error
				%>
				<span class="notify">Please enter a positive integer for employee number</span>
				<%
				
			} else {
				
			
			
				// extract permission
				CommonValues.permissions permission = CommonValues.permissions.valueOf(employeeData.getPermission());
	
				// extract controller from session
				AdministratorController controller = (AdministratorController) session.getAttribute(WebCommons.CONTROLLER);
	
				try {
	
					// try adding employee
					boolean couldAdd = controller.addEmployee(
							employeeData.getNumberValue(),
							employeeData.getName(), employeeData.getUserName(),
							permission);
	
					// check success
					if (!couldAdd) {
	
						// display error message
						%>
						<span class="notify">Sorry, could not add employee- duplicate entry...
						<br>Please check employee number or username</span>
						<%
					} else {
	
						// success
						%>
						<span class="notify"><br>Added successfully!</span>
						<%
					}
	
				} catch (DBException e) {
	
					// move to error page
					response.sendRedirect("../../commonPages/ErrorPage.jsp");
				}
			}
		}
	%>
</body>
</html>